package abc.red.controller;

import abc.red.common.BaseContext;
import abc.red.entity.*;
import abc.red.service.PurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName PurchaseController
 * @Author YiXia
 * @Date 2024/1/19 12:01
 * @Version 1.0
 * @Description TODO
 **/

@Slf4j
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService service;
    private int pageSize = 20;




    @PostMapping("/inside")
    public R<String> add(@RequestBody Purchase p) {

        p.setMasterId(BaseContext.getCurrentId());
        boolean end = service.save(p);
        if (end) {
            log.info("打印 purchaseController-inside 方法上传的 Purchase 对象数据   {}", p.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }





    @PostMapping("/getLog")
    public R<IPage> getLog(D d) {

        LambdaQueryWrapper<Purchase> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Purchase::getMasterId,BaseContext.getCurrentId());
        IPage<Purchase> page = new Page<>(d.getNum1(), pageSize);
        Long id=BaseContext.getCurrentId();

        service.page(page);
        if(id==1){
            return R.success(service.page(page));
        }else{
            return R.success(service.page(page,lqw));
        }
    }



    /**
     * TODO 先计算入库，在计算出库，最后才是剩余。
     * */
    @PostMapping("/getAllByWarehouseId")
    public R<IPage> getAllByWarehouseId(@RequestBody D d) {
        //超级管理员可以查询所有
        log.info("更根据仓库id查询仓库名下所有入库信息  参数D={}",d);
        LambdaQueryWrapper<Purchase> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Purchase::getWarehouseId,d.getId());
        IPage<Purchase> page = new Page<>(d.getNum1(), pageSize);
        service.page(page,lqw);
        return R.success(page);
    }









}
